var num = GetQueryString("commodityNumber");

$(function() {

	$(".leibie .list").on('tap', function() {
		$(".leibie .list").toggleClass("active");
	})
	$(".leibie1 .list").on('tap', function() {
		$(".leibie1 .list").toggleClass("active");
	})
	addminuFn();
	
	YouLikeFn(); //推荐商品

	AjaxNoLGettFn("api/bizcommodity/selectbynumber?adcode=100000&commoditynumber=" + num, {}, function(data) {
		//		查询商品详情
		if(data.htmlState.status == 200) {
			var obj = data.dataResultObj;
			//商品图片mainPhoto
			$(".PD_banner div").css("background-image", "url(" + IMGUrl + '' + obj.mainPhoto + ") ");

			//商品基础信息
			var presentPrice = (obj.dataResultObj.presentPrice / 100).toFixed(2); //价格
			var salesVolume = obj.salesVolume ? obj.salesVolume : 0;
			$("#pdetailcont").html('<div class="PD_name product_font">' + obj.title + '</div>' +
				'<div class="PD_price">￥' + presentPrice + '</div>' +
				'<div class="PD_button">' +
				'<button class=" add_car">加入购物车</button>' +
				'<button class=" gouwuche">立即购买</button>' +
				'</div>');

			//商品参数
			var comobj = obj.bizCommoditys[0];
			$("#PD_logistics2").html('<div class="PD_parameter"><span>货源地</span><span class="PD_font commodityOrigin">' + comobj.commodityOrigin + '</span></div>' +
				'<div class="PD_bottom"></div>' +
				'<div class="PD_parameter"><span>产品类型</span><span class="PD_font commodityApply">' + comobj.commoditySet + '</span></div>' +
				'<div class="PD_bottom"></div>' +
				'<div class="PD_parameter"><span>规格</span><span class="PD_font commoditySet">' + comobj.commodityModel + '</span></div>' +
				'<div class="PD_bottom"></div>' +
				'<div class="PD_parameter" style="height:auto"><div class="PD_purpose">用途</div><div class="PD_purpose PD_purpose2">' + comobj.commodityApply + '</div></div>' +
				'<div class="PD_bottom"></div>' +
				'<div class="PD_parameter notice"div>交易须知</div><div class="comment">地区配送时间可能存在偏差</div></div>');

			//parseFloat(oneItem.dataResultObj.presentPrice/100).toFixed(2) 价钱
			$("#pprice").html(presentPrice)
			$(".proimg img").attr('src', IMGUrl + '' + obj.mainPhoto);
			$("#pprice").attr("data-unitPrice", presentPrice);
			var neednum = $("#needNumber").val();
			var price = $("#pprice").attr("data-unitPrice");
			$("#pprice").html(neednum * price);

			//添加浏览记录
			var previewPrice = $("#pprice").html() * 100;
			MuiAjaxFn("client/record/insertrecord", {
				'commodityNumber': num,
				'previewPrice': previewPrice
			}, function(data) {

			})

			//	修改数量
			$("#Paddnum ").on('tap', function() {
				//所需的数量
				setTimeout(function() {
					var neednum = $("#needNumber").val();
					var price = $("#pprice").attr("data-unitPrice");
					console.log(neednum + '---' + price)
					$("#pprice").html(neednum * price)
				}, 500);

			});
			
			
			$("#needNumber").on('tap',function(){
				//点击输入数量框的弹出
				
			})

			$(".add_car").on("tap", function() {
				//		点击加入购物车
				var mask = mui.createMask(function() {
					$(".openbox").toggle();
				}); //callback为用户点击蒙版时自动执行的回调；
				mask.show(); //显示遮罩
				$(".openbox").toggle();

				//			点击弹出框内的确定-最终加入购物车
				$(".yesbtn-bottom").on("tap", function() {
					mask.close(); //关闭遮罩
					$('.openbox').hide();

					MuiAjaxFn("client/detailed/insertdetailed", {
						'commodityNumber': num,
						'needNumber': $("#needNumber").val(),
						'add_price': $("#pprice").html(),
						'add_time': new Date().getTime(),
						'newNeedNumber': $("#needNumber").val()
					}, function(data) {
						if(data.htmlState.status == 200) {
							mui.toast('加入购物车成功');
						}

					});
				});

			});
			
			
			

			//立即购买
			$(".gouwuche").on("tap", function() {

				var mask = mui.createMask(function() {
					$(".openbox").toggle();
				}); //callback为用户点击蒙版时自动执行的回调；
				mask.show(); //显示遮罩
				$(".openbox").toggle();

				//			点击弹出框内的确定-最终加入购物车
				$(".yesbtn-bottom").on("tap", function() {
					if(parseInt($("#needNumber").val()) <= 0){
						mui.toast('请输入大于0的购买数量');
					}else{
						mask.close(); //关闭遮罩
					$('.openbox').hide();

					var result = []; //购物清单

					result.push({
						'commodityNumber': num,
						'count': $("#needNumber").val(),
						'allprice': $("#pprice").val(),
						'allcount': $("#needNumber").val()
					});

					if(result.length > 0) { //当购物清单长度大于0，即可进入“确认订单”页面
						result["present"] = false;
						result = JSON.stringify(result); //转化为字符串
						console.log("转换成功！" + result);
						localStorage.setItem('goodsList', result);
						window.location.href = "order_confirm.html";
					}
					}
					
				})

			});
		} else {
			//			alert("数据异常,请刷新后重试")
		}

	})

	//评价
	AjaxNoLPostFn('api/orderinfo/evaluate/selectbycommodity?page=1&pagesize=100', {
		'commodityNumber': num,
		'tosort': 'desc',
		'page': '0',
		'pagerow': '100',
		'sorting': 'commodity_star'
	}, function(data) {
		if(data.htmlState.status == 200) {
			var str = '';
			$("#evaluatelist").empty();
			if(data.dataResultList != '') {
				$.each(data.dataResultList, function(idx, obj) {
					str +=
						'<div class="PD_information">'+
						'<div class="PD_font product_font">'+obj.evaluateText+'</div>'+
						'<div class="mui-content-padded">'+
						'	<div class="icons mui-inline">';
						for(var i=0 ; i< obj.commodityStar ; i++){
							str += '<i class="mui-icon mui-icon-star-filled"></i>'
						}
						for(var j = obj.commodityStar ;j<5 ;j++){
							str += '<i class="mui-icon mui-icon-star"></i>';
						}
						str += '	</div>'+
						'</div>'+
						'</div>' ;

				})

			} else {
				str += '<p style="padding: 10px 15px;color: #666;">暂无评价</p>'
			}
			$("#evaluatelist").append(str);

		}

	});
	$("#collction").on("tap", function() {
		//点击收藏商品
		AddCollctionFn(num);

	});

});
